package com.atguigu.web.config;



import com.alibaba.druid.pool.DruidDataSource;
import com.atguigu.dao.UserDao;
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * Spring不扫描controller组件、AOP咋实现的....
 */
@ComponentScan(value = "com.atguigu.web",excludeFilters = {
		@ComponentScan.Filter(type= FilterType.ANNOTATION,value = {Controller.class,EnableWebMvc.class})
})
//@EnableTransactionManagement
@Configuration
public class SpringConfig {




//	@Bean
//	public DruidDataSource druidDataSource(){
//		DruidDataSource source = new DruidDataSource();
//		source.setDriverClassName("com.mysql.jdbc.Driver");
//		source.setUrl("jdbc:mysql://192.168.16.151:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC");
//		source.setUsername("root");
//		source.setPassword("T.123456");
//		return source;
//	}
//	@Bean
//	public JdbcTemplate jdbcTemplate(DataSource dataSource){
//		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//		return jdbcTemplate;
//	}
//	@Bean
//	public TransactionManager transactionManager(DataSource dataSource){
//		DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
//		return dataSourceTransactionManager;
//	}
//
//	@Bean
//	public MapperScannerConfigurer mapperScannerConfigurer(){
//		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
//		mapperScannerConfigurer.setBasePackage("com.atguigu.dao");
//		return mapperScannerConfigurer;
//	}
//
//	@Bean
//	public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws FileNotFoundException {
//		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//		sqlSessionFactoryBean.setTypeAliasesPackage("com.atguigu.bean");
//		sqlSessionFactoryBean.setDataSource(dataSource);
//		sqlSessionFactoryBean.setConfigLocation(new InputStreamResource(new FileInputStream("C:\\project\\spring-frameworks\\springmvc-source-test\\src\\main\\resources\\SqlMapperConfig.xml")));
//		return sqlSessionFactoryBean;
//	}
//
//	@Bean
//	public MapperFactoryBean<UserDao> getMap(SqlSessionFactoryBean sqlSessionFactoryBean) throws Exception {
//		MapperFactoryBean<UserDao> mapperFactoryBean=new MapperFactoryBean<UserDao>();
//		mapperFactoryBean.setMapperInterface(UserDao.class);
//		mapperFactoryBean.setSqlSessionFactory( sqlSessionFactoryBean.getObject());
//		return mapperFactoryBean;
//	}
	//平台事务管理
	//管理事务
//	@Bean
//	public PlatformTransactionManager getPlat(DataSource dataSource){
//		PlatformTransactionManager platformTransactionManager=new DataSourceTransactionManager(dataSource);
//		return platformTransactionManager;
//	}

}
